<template>
  <div class="demo-app">
    <el-row style="padding: 20px 20px 0px 20px;">
      <el-col :span="24">
        <el-button-group>
          <el-button :loading="up" @click="lastWeek" type="primary" icon="el-icon-arrow-left">上一周</el-button>
          <el-button
            :loading="down"
            @click="nextWeek"
            type="primary"
            icon="el-icon-arrow-right el-icon--right"
          >下一周</el-button>
        </el-button-group>
        <el-button
          style="float:right;margin-left:10px;"
          :loading="substr"
          icon="el-icon-top"
          @click="openDeptWork"
          type="primary"
          round
        >部门事务</el-button>
        <el-button
          style="float:right"
          :loading="substr"
          icon="el-icon-top"
          @click="supplement"
          type="primary"
          round
        >日报补填</el-button>
        <el-button
          style="float:right"
          :loading="substr"
          icon="el-icon-top"
          @click="submit"
          type="primary"
          round
        >提交</el-button>
      </el-col>
    </el-row>
    <el-table
      border
      style="width: 100%;margin-top: 20px;height: 900px;"
      v-loading="loading"
      element-loading-text="加载中..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :data="tableData"
      :span-method="spanMethod"
    >
      <template v-slot:empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
      <el-table-column align="center" label="日期" width="120">
        <template slot-scope="scope">
          <i class="el-icon-date">
            <b>{{scope.row.workDate}}</b>
          </i>
          <span :hidden="scope.row.workDate != now3">
            <br />
            <font style="color: deepskyblue;">
              <b>(今天)</b>
            </font>
          </span>
          <span>
            <br />
            <font
              style="color: green;"
            >星期{{"日一二三四五六".charAt(new Date(scope.row.workDate).getDay())}}</font>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="projectName" align="center" label="所属项目" width="200">
        <template
          slot-scope="scope"
        >{{(!scope.row.planId&&!scope.row.status)? `您的上级领导尚未给您安排${scope.row.workDate}的工作计划，暂无法填报，请联系您的上级领导安排工作计划！`:scope.row.projectName}}</template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="planWorkType"
        label="工作事项"
        width="150"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="planTask" align="center" label="工作安排"></el-table-column>
      <el-table-column align="center" label="工作小时数" width="160">
        <template slot-scope="scope">
          <span v-if="scope.row.isAllowEdit == 0 && !scope.row.status">
            <font style="color: red;">您已超时未填报，请通过日报补填流程进行填报！</font>
          </span>
          <span v-else-if="scope.row.isAllowEdit == 0">{{scope.row.workHours}}小时</span>
          <el-input-number
            v-else
            :disabled="scope.row.isAllowEdit == 0||scope.row.workDate > now3"
            v-model="scope.row.workHours"
            :precision="1"
            :step="0.5"
            :max="7.5"
            :min="0"
          ></el-input-number>
          <span v-if="scope.row.status=='Approved'">
            <br />
            <font style="color: green;">已审批</font>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="工作内容" style="height:100%">
        <template slot-scope="scope">
          <span v-if="scope.row.isAllowEdit == 0">{{scope.row.workTask}}</span>
          <el-input
            v-else
            :disabled="scope.row.isAllowEdit == 0 ||scope.row.workDate > now3"
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 6}"
            placeholder="请输入内容"
            v-model="scope.row.workTask"
          ></el-input>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import _ from "lodash";
import request from "@/utils/request";
export default {
  components: {},
  created: function () {
    this.getData(this.now);
    this.getSelectData();
  },
  data: function () {
    return {
      loading: false,
      //当前时间,作为分页的条件
      now: new Date(),
      //区分
      now3: formatter.formatDate(new Date()),
      //下拉选择项目
      projectData: null,
      //表格数据
      tableData: null,
      //按钮状态
      up: false,
      down: false,
      substr: false,
    };
  },
  methods: {
    supplement() {
      this.$root.openModal("/smartform/workflow/SupplementsFrom");
    },
    //获取下拉资源
    getSelectData() {
      request({
        url: "/eip/projectWorkload/getSelect",
        method: "get",
        params: {},
      })
        .then((r) => {
          if (r && !r.initMessage) {
            this.projectData = r.parameters.project;
          } else {
            this.$message.error(r.initMessage || "数据请求失败");
          }
        })
        .catch((e) => {
          reject(e);
        });
    },
    //根据当前时间获取上下周的信息
    getData(now) {
      this.loading = true;
      //格式化
      now = formatter.formatDateTime(now);
      request({
        url: "/eip/projectWorkload/getWorkLoad",
        method: "get",
        params: { time: now },
      })
        .then((r) => {
          if (r && !r.initMessage) {
            this.tableData = r.parameters.workLoads;
            this.up = false;
            this.dowm = false;
            this.loading = false;
          } else {
            this.$message.error(r.initMessage || "数据请求失败");
          }
        })
        .catch((e) => {
          reject(e);
        });
    },
    lastWeek() {
      this.up = true;
      let time = this.now;
      let dateTime = time.setDate(time.getDate() - 7);
      this.now = new Date(dateTime);
      this.getData(this.now);
    },
    nextWeek() {
      this.dowm = true;
      let time = this.now;
      let dateTime = time.setDate(time.getDate() + 7);
      this.now = new Date(dateTime);
      this.getData(this.now);
    },
    submit() {
      var needSave = false;
      var z = 1;
      //克隆
      var arr = _.cloneDeep(this.tableData);
      for (var j = 0; j < arr.length; ) {
        var t = _.filter(arr, { workDate: arr[j].workDate });
        var sum = 0;
        for (var y = 0, len1 = t.length; y < len1; y++) {
          var e = t[y];
          //转成字符串
          e.workHours = _.toString(e.workHours);
          if (e.isAllowEdit == 1) {
            // if (!_.isEmpty(e.workHours) && !_.isEmpty(e.workTask)) {
            //   if (_.toFinite(e.workHours) <= 0) {
            //     this.$message({
            //       message: "请检查工作时长！",
            //       type: "info",
            //     });
            //     return;
            //   }
            // }
            // if (
            //   (_.isEmpty(e.workHours) && !_.isEmpty(e.workTask)) ||
            //   (!_.isEmpty(e.workHours) && _.isEmpty(e.workTask))
            // ) {
            //   this.$message({
            //     message: "不能只填写工作小时数或工作内容喔！",
            //     type: "info",
            //   });
            //   return;
            // }
            needSave = true;
          }
          if (typeof t[y].workHours != "undefined") {
            sum = sum + _.toFinite(t[y].workHours);
          }
        }
        if (sum > 7.5) {
          z = 0;
        }
        arr.splice(j, t.length);
      }
      if (z == 0) {
        this.$message({
          message: "每天的工作时长总和不能超过7.5！请检查数据",
          type: "error",
        });
        return;
      }
      if (!needSave) {
        this.$message({
          message: "没有需要保存的数据",
          type: "warning",
        });
        return;
      }
      request({
        url: "/eip/projectWorkload/saveWorkLoad",
        method: "post",
        data: { weekWorkload: this.tableData },
      })
        .then((r) => {
          if (r && !r.initMessage) {
            if (r.success == true) {
              this.$message({
                message: "保存成功！",
                type: "success",
              });
            } else {
              this.$message.error("保存失败！");
            }
            this.getData(this.now);
          } else {
            this.$message.error(r.initMessage || "数据请求失败");
          }
        })
        .catch((e) => {
          reject(e);
        });
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if (!row.planId && !row.status) {
        if (columnIndex == 0) {
          return [1, 1];
        } else if (columnIndex == 1) {
          return [1, 5];
        } else {
          return [0, 0];
        }
      } else if (row.isAllowEdit == 0 && !row.status) {
        if (columnIndex < 4) {
          return [1, 1];
        } else if (columnIndex == 4) {
          return [1, 2];
        } else {
          return [0, 0];
        }
      }
    },
    openDeptWork() {
      this.$root.openModal(
        "/smartform/hub/DeptWorkloadForm",
        () => {
          this.getData(this.now);
        },
        {
          date: this.$formatter.formatDate(new Date()),
          hour: 7.5,
          userId: null,
        },
        undefined,
        500
      );
    },
  },
};
</script>

<style lang='css' scoped>
</style>
